package com.ruoyi.web.controller.video;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.video.domain.BusCarouselInfo;
import com.ruoyi.video.domain.vo.CarouselInfoParam;
import com.ruoyi.video.service.ICarouselInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播图 信息操作处理
 * 
 * @author ruoyi
 */
@Api("轮播图信息管理")
@RestController
@RequestMapping("/bus/carousel")
public class BusCarouselController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(BusCarouselController.class);

    @Autowired
    private ICarouselInfoService carouselInfoService;

    /**
     * 获取轮播图列表
     */
    @ApiOperation("获取轮播图列表")
    @PreAuthorize("@ss.hasPermi('bus:carousel:list')")
    @GetMapping("/list")
    public TableDataInfo list() {
        List<BusCarouselInfo> list = carouselInfoService.selectCarouselList();
        return getDataTable(list);
    }

    /**
     * 根据轮播图编号获取详细信息
     */
    @ApiOperation("获取轮播图详情")
    @PreAuthorize("@ss.hasPermi('bus:carousel:query')")
    @GetMapping(value = "/{carouselId}")
    public AjaxResult getInfo(@PathVariable Long carouselId) {
        return AjaxResult.success(carouselInfoService.selectCarouselById(carouselId));
    }

    /**
     * 新增轮播图
     */
    @ApiOperation("保存轮播图信息")
    @PreAuthorize("@ss.hasPermi('bus:carousel:add')")
    @Log(title = "轮播图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CarouselInfoParam param) {
        BusCarouselInfo carouselInfo = new BusCarouselInfo();
        try {
            carouselInfo.setCarouselUrl(param.getCarouselUrl());
            carouselInfo.setCarouselPath(param.getCarouselPath());
            carouselInfo.setCreateBy(SecurityUtils.getUsername());
            carouselInfo.setUpdateBy(SecurityUtils.getUsername());
            carouselInfoService.insertCarousel(carouselInfo);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除轮播图
     */
    @ApiOperation("删除轮播图信息")
    @PreAuthorize("@ss.hasPermi('bus:carousel:remove')")
    @Log(title = "轮播图", businessType = BusinessType.DELETE)
    @DeleteMapping("/{carouselId}")
    public AjaxResult remove(@PathVariable("carouselId") Long carouselId) {
        return toAjax(carouselInfoService.deleteCarouselById(carouselId));
    }
}
